package ballena.utiles;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.util.Log;

public class Servlet {
	String ruta;
	String parametros[];

	OutputStream out;
	InputStream in;

	public String getRuta() {
		return ruta;
	}

	public void setRuta(String ruta) {
		this.ruta = ruta;
	}

	public Servlet(String r, String parametros[]) {
		ruta = r;
		ruta = ruta + "?";
		int n = 0;
		while (n < parametros.length) {
			ruta = this.ruta + parametros[n];
			if (n != parametros.length - 1) {
				ruta = ruta + "&";
			}
			n++;
		}
	}
	
	public Servlet(String r, String parametros[], String idSesion) {
		ruta = r;
		ruta = ruta + ";jsessionid=" + idSesion;
		ruta = ruta + "?";
		int n = 0;
		while (n < parametros.length) {
			ruta = this.ruta + parametros[n];
			if (n != parametros.length - 1) {
				ruta = ruta + "&";
			}
			n++;
		}
	}

	public void conectar() {
		try {

			URL ob2 = new URL(ruta);

			URLConnection conn2 = ob2.openConnection();

			conn2.setDoInput(true);
			conn2.setDoOutput(true);
			
			out = conn2.getOutputStream();
			in = conn2.getInputStream();


		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public InputStream getInputStream() {
		return in;
	}

	public OutputStream getOutputStream() {
		return out;
	}

	public void desconectar() {
		try {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
